package com.code.factory.utils;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class DBUtil {
    private Connection conn;

    public DBUtil() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        	//Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void openConnection() throws SQLException {
    	Properties props = new Properties();
    	props.put("user", Const.JDBC_USERNAME);
    	props.put("password", Const.JDBC_PASSWORD);
    	props.put("remarks","true");
    	conn = DriverManager.getConnection(Const.JDBC_URL, props);
        //conn = DriverManager.getConnection(Const.JDBC_URL, Const.JDBC_USERNAME, Const.JDBC_PASSWORD);
    }
    
//    private void openConnection2() throws SQLException {
//        conn = DriverManager.getConnection(Const.JDBC_URL2, Const.JDBC_USERNAME, Const.JDBC_PASSWORD);
//    }

    private void closeConnection() throws SQLException {
        if (conn != null) {
            conn.close();
        }
    }
    /**
     * 遍历表中所有列
        * @Title: queryMetaData
        * @param @param tableName
        * @param @return    参数
        * @return Map<String,Object>    返回类型
        * @throws
     */
    public Map<String, Object> queryMetaData(String tableName) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            this.openConnection();
            DatabaseMetaData dmd = conn.getMetaData();
        	ResultSet colRet = dmd.getColumns(null, "%", tableName, "%");
            List<Map<String, String>> columns = new ArrayList<Map<String, String>>();
            while (colRet.next()) {
                Map<String, String> column = new HashMap<String, String>();
                if(columns.isEmpty()){
                	String columnName = colRet.getString("COLUMN_NAME");
                    String columnType = colRet.getString("TYPE_NAME");
                    String columnComment = colRet.getString("REMARKS");
                    column.put("oldColumnName", columnName);
                    column.put("columnComment", columnComment);
                    column.put("columnName", DBtoJavaUtil.formatFieldName(columnName));
                    column.put("setMethodName", DBtoJavaUtil.formatSetMethodName(columnName));
                    column.put("getMethodName", DBtoJavaUtil.formatGetMethodName(columnName));
                    column.put("columnType", DBtoJavaUtil.toJavaType(columnType));
                    columns.add(column);
                    params.put(columnName, column);
                }else{
                	if(!params.containsKey(colRet.getString("COLUMN_NAME"))){
                		String columnName = colRet.getString("COLUMN_NAME");
                        String columnType = colRet.getString("TYPE_NAME");
                        String columnComment = colRet.getString("REMARKS");
                        column.put("oldColumnName", columnName);
                        column.put("columnComment", columnComment);
                        column.put("columnName", DBtoJavaUtil.formatFieldName(columnName));
                        column.put("setMethodName", DBtoJavaUtil.formatSetMethodName(columnName));
                        column.put("getMethodName", DBtoJavaUtil.formatGetMethodName(columnName));
                        column.put("columnType", DBtoJavaUtil.toJavaType(columnType));
                        columns.add(column);
                        params.put(columnName, column);
                    }
                }
            }
            map.put("columns", columns);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                this.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    /**
     * 查询所有表结构
     * 
     * @return
     */
    public List<String> queryAllTablesData() {
        List<String> list = new ArrayList<String>();
        try {
            this.openConnection();
            DatabaseMetaData dmd = conn.getMetaData();
            ResultSet tbRet = dmd.getTables(null, "%", "", new String[] { "TABLE" });
            while (tbRet.next()) {
                list.add(tbRet.getString("TABLE_NAME"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                this.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
//        try {
//            this.openConnection2();
//            DatabaseMetaData dmd = conn.getMetaData();
//            ResultSet tbRet = dmd.getTables(null, "%", "", new String[] { "TABLE" });
//            while (tbRet.next()) {
//                list.remove(tbRet.getString("TABLE_NAME"));
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                this.closeConnection();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
        return list;
    }
}
